package com.ybkj.daijia.server.event.listener;

import com.ybkj.daijia.common.Principal;
import com.ybkj.daijia.server.event.model.OperationLogEvent;
import com.ybkj.daijia.server.model.OperationLog;
import com.ybkj.daijia.server.service.OperationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class OperationLogEventListener implements ApplicationListener<OperationLogEvent> {

    @Autowired
    private OperationLogService operationLogService;


    @Override
    @Async
    public void onApplicationEvent(OperationLogEvent event) {

        Principal principal = (Principal) event.getSource();
        String detail = event.getDetail();

        OperationLog operationLog = new OperationLog();

        operationLog.setCompamyId(principal.getCompanyId());
        operationLog.setCompanyName(principal.getCompanyAbbreviation());
        operationLog.setOperating(principal.getUsername());
        operationLog.setHost(principal.getHost());
        operationLog.setDetail(String.format(detail, principal.getUsername()));

        operationLogService.insertRecord(operationLog);

    }

}
